简体   繁体   中英

How to pause play and stop audio file Streaming From parse.com (android)?

I am trying to play audio in my app from parse.com. I'm able to start and play the Media file but cant pause and stop it. Here is the code:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
query.getInBackground(ObjId, new GetCallback<ParseObject>() {
public void done(ParseObject recording, com.parse.ParseException e) {
    if (e != null) { 
       //do nothing
    }
    else {
        ParseFile audioFile = recording.getParseFile("Audio"); 
        String audioFileURL = audioFile.getUrl();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(audioFileURL);
            mediaPlayer.prepare();
              //mediaPlayer.start();
           mediaPlayer.start();

          finalTime = mediaPlayer.getDuration();
          startTime = mediaPlayer.getCurrentPosition();
        } catch (IllegalArgumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (SecurityException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }        
    }
}
}); 

You're not playing a live stream, the file plays after the object is downloaded as you're setting up the action inside the

public void done(ParseObject recording, com.parse.ParseException e)

and this method is executed in another thread which complicates the situation aa lot, you should get the file first with:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
ParseObject obj = query.getFirst()
ParseFile audioFile = obj.getParseFile("Audio"); 
        String audioFileURL = audioFile.getUrl();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

And then implement your other features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM