Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mediaPlayer error -38,0

I try to do simple online radio player. Here is adress of stream http://radio-electron.ru:8000/96 Here is my code.

MyActivity.java

package com.example.untitled2;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MyActivity extends Activity {

    MediaPlayer mediaPlayer;
    Button playButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mediaPlayer = new MediaPlayer();
        playButton = (Button)findViewById(R.id.button);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer.isPlaying())
                    mediaPlayer.stop();
                else {
                    try {

                        mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("http://radio-electron.ru:8000/96"));
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                            @Override
                            public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                                Toast.makeText(getApplicationContext(), "ERROR " + i, Toast.LENGTH_LONG).show();
                                playButton.setEnabled(false);
                                Log.d("radio", "error " + i + " " + i2);
                                return false;  //To change body of implemented methods use File | Settings | File Templates.
                            }
                        });
                        mediaPlayer.prepareAsync();
                    }
                    catch (IOException e) {
                        Toast.makeText(getApplicationContext(), "ERROR " + e, Toast.LENGTH_LONG).show();
                        Log.d("radio", "error " + e);
                    }
                    mediaPlayer.start();
                }
            }
        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
        >
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_gravity="center"/>
</FrameLayout>

I have no idea, why i get -(38, 0) code and what does it mean. I using Intellij IDEA and trying this code on Android 2.3 and 4.2 emulators and get some problem.

like image 271
Coma White Avatar asked Aug 28 '13 07:08

Coma White


5 Answers

-38 refers to ENOSYS error code from errno.h (see this explanation https://stackoverflow.com/a/15206308/768935)

You seem to try to start the playing before the preparation is complete. Use the setOnPreparedListener() method to set a preparation listener and call the start() method only after the preparation is complete.

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  public void onPrepared(MediaPlayer mp) {
      mp.start();
  }
});
mediaPlayer.prepareAsync();

And remove the current mediaPlayer.start() invocation from the code.

like image 91
allprog Avatar answered Nov 05 '22 09:11

allprog


I was getting my -38 error by calling getDuration(); on the MediaPlayer before it was prepared.


It's worth checking the MediaPlayer doc.

There is a paragraph that starts It is a programming error to invoke methods such as getCurrentPosition()...

that has a list of methods which are un-ideal to call before the MediaPlayer is prepared, which in turn may result in -38.

like image 44
Unknownweirdo Avatar answered Nov 05 '22 10:11

Unknownweirdo


You better check if you are executing any operation that is related to the playing state like getCurrentPosition() before the Mediaplayer is started.

like image 3
e_lwj Avatar answered Nov 05 '22 09:11

e_lwj


The error code -38 ought to correspond to INVALID_OPERATION.

A likely cause of this is that you don't wait for prepareAsync to finish before you call start. You should set an onPreparedListener and start the MediaPlayer only when onPrepared has been called.

like image 2
Michael Avatar answered Nov 05 '22 09:11

Michael


@allprog and @Michael are right. But there is another way, if you don't want to use prepareAsync(), use prepare(). That is blocking, which is returned only when it is prepared.

like image 2
g-hos Avatar answered Nov 05 '22 10:11

g-hos