Skip to content

Commit

Permalink
Add Android MediaPlayer example
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Feb 18, 2018
1 parent abe48e2 commit d983520
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/controllers/android/mediaplayer.js
@@ -0,0 +1,34 @@

var Activity = require('android.app.Activity');
var AudioManager = require('android.media.AudioManager');
var MediaPlayer = require('android.media.MediaPlayer');
var Uri = require('android.net.Uri');
var activity = new Activity(Ti.Android.currentActivity);
var context = activity.getApplicationContext();
var mMediaPlayer;

(function(container) {
var contentUri = Uri.parse('android.resource://' + activity.getPackageName() + '/raw/audio');

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener({
onCompletion: function (mediaPlayer) {
Ti.API.info('MediaPlayer playback completed');
}
}));
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(context, contentUri);
mMediaPlayer.prepare();
})($.window);

function startMedia() {
mMediaPlayer.start();
}

function stopMedia() {
mMediaPlayer.pause();

// NOTE: You can also stop it, but then you have to prepare() it again as well
// mMediaPlayer.stop();
// mMediaPlayer.prepare();
}
Binary file added app/platform/android/res/raw/audio.mp3
Binary file not shown.
6 changes: 6 additions & 0 deletions app/views/android/mediaplayer.xml
@@ -0,0 +1,6 @@
<Alloy>
<Window id="win" title="Media Player">
<Button top="100" title="Start Media" onClick="startMedia" />
<Button top="200" title="Stop Media" onClick="stopMedia" />
</Window>
</Alloy>
1 change: 1 addition & 0 deletions app/views/index.xml
Expand Up @@ -17,6 +17,7 @@
<ListItem itemId="devicecapabilities" platform="windows" title="Device Capabilities"/>
<ListItem itemId="label" title="Styled Labels"/>
<ListItem itemId="beacons" platform="ios" title="iBeacons"/>
<ListItem itemId="mediaplayer" platform="android" title="Media Player"/>
<ListItem itemId="videoplayer" title="Video Player"/>
<ListItem itemId="drawrect" platform="android,ios,windows" title="Custom Drawing"/>
<ListItem itemId="touches" title="Touches"/>
Expand Down

0 comments on commit d983520

Please sign in to comment.