Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create media player (using url)

For playing online music I am getting the exception in do in background. I am unable to remove the problem.

01-08 09:42:05.199: E/MediaPlayer(16257): Unable to to create media player
01-08 09:42:05.209: W/System.err(16257): java.io.IOException: setDataSource failed.: status=0x80000000
01-08 09:42:05.229: W/System.err(16257):    at android.media.MediaPlayer._setDataSource(Native Method)
01-08 09:42:05.229: W/System.err(16257):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1000)  
01-08 09:42:05.229: W/System.err(16257):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:960)
01-08 09:42:05.229: W/System.err(16257):    at com.example.audioplayer.MainActivity$Player.doInBackground(MainActivity.java:89)
01-08 09:42:05.229: W/System.err(16257):    at com.example.audioplayer.MainActivity$Player.doInBackground(MainActivity.java:1)  
01-08 09:42:05.229: W/System.err(16257):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-08 09:42:05.229: W/System.err(16257):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-08 09:42:05.229: W/System.err(16257):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 09:42:05.229: W/System.err(16257):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 09:42:05.239: W/System.err(16257):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 09:42:05.239: W/System.err(16257):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 09:42:05.239: W/System.err(16257):    at java.lang.Thread.run(Thread.java:856)
01-08 09:42:05.389: D/Prepared(16257): //false
01-08 09:42:05.389: E/MediaPlayer(16257): start called in state 1
01-08 09:42:05.389: E/MediaPlayer(16257): error (-38, 0)
01-08 09:42:05.449: E/MediaPlayer(16257): Error (-38,0)

This is the code:

public class MainActivity extends Activity {
private Button btn;
/**
* help to toggle between play and pause.
*/
private boolean playPause;
private MediaPlayer mediaPlayer;
/**
* remain false till media is not completed, inside OnCompletionListener make it true.
*/
private boolean intialStage = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn.setOnClickListener(pausePlay);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private OnClickListener pausePlay = new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub

    if (!playPause) {
        btn.setBackgroundResource(R.drawable.button_pause);
        if (intialStage)
            new Player()
                    .execute("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
        else {
            if (!mediaPlayer.isPlaying())
                mediaPlayer.start();
        }
        playPause = true;
    } else {
        btn.setBackgroundResource(R.drawable.button_play);
        if (mediaPlayer.isPlaying())
            mediaPlayer.pause();
        playPause = false;
    }
}
};
/**
* preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
* @author piyush
*
*/

class Player extends AsyncTask<String, Void, Boolean> {
private ProgressDialog progress;

@Override
protected Boolean doInBackground(String... params) {
    // TODO Auto-generated method stub
    Boolean prepared;
    try {

        mediaPlayer.setDataSource(params[0]);//line 89

        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                intialStage = true;
                playPause=false;
                btn.setBackgroundResource(R.drawable.button_play);
                mediaPlayer.stop();
                mediaPlayer.reset();
            }
        });
        mediaPlayer.prepare();
        prepared = true;
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        Log.d("IllegarArgument", e.getMessage());
        prepared = false;
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        prepared = false;
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        prepared = false;
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        prepared = false;
        e.printStackTrace();
    }
    return prepared;
}

@Override
protected void onPostExecute(Boolean result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if (progress.isShowing()) {
        progress.cancel();
    }
    Log.d("Prepared", "//" + result);
    mediaPlayer.start();

    intialStage = false;
}

public Player() {
    progress = new ProgressDialog(MainActivity.this);
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    this.progress.setMessage("Buffering...");
    this.progress.show();

}
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mediaPlayer != null) {
    mediaPlayer.reset();
    mediaPlayer.release();
    mediaPlayer = null;
}
}
}

The link is correct. I have mentioned the exception generated on line 89. Please see what is been wrong at there.

Thanks in advance...

like image 527
Vivek Warde Avatar asked Jan 08 '15 04:01

Vivek Warde


1 Answers

You don't need to write your own AsyncTask to handle this, the MediaPlayer class already has a PrepareAsync method, which will handle background loading for you.

Try something like this:

package com.mypackage.test;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends Activity {

    private Button btn;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }
            }
        });

    }
}

I've also simplified some of the code for my own sake.

Also, don't forget to include the INTERNET permission in the manifest:

<uses-permission android:name="android.permission.INTERNET"/>

like image 138
Tim Malseed Avatar answered Sep 26 '22 05:09

Tim Malseed