-
Notifications
You must be signed in to change notification settings - Fork 252
/
MediaPlayer.txt
121 lines (104 loc) · 3.47 KB
/
MediaPlayer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//permission need
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
//layout degin
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:id="@+id/bustart"
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/buStop" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/buPause" />
</LinearLayout>
</LinearLayout>
//code need
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
namespace Android_Media
{
[Activity(Label = "Android_Media", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//define timer
System.Timers.Timer t;
//define seek bar
SeekBar seekBar1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//set timer
t = new System.Timers.Timer();
t.Interval = 3000;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
seekBar1 = FindViewById<SeekBar>(Resource.Id.seekBar1);
//prepar audio
string filePath = "http://server6.mp3quran.net/thubti/001.mp3";
PreparePlayer(filePath);
//start
Button button = FindViewById<Button>(Resource.Id.bustart);
button.Click += delegate {
player.Start();
t.Start();
};
//stop
Button buStop = FindViewById<Button>(Resource.Id.buStop);
buStop.Click += delegate {
player.Stop();
t.Stop();
PreparePlayer(filePath);
};
//pause
Button buPause = FindViewById<Button>(Resource.Id.buPause);
buPause.Click += delegate {
player.Pause();
t.Stop();
};
}
protected MediaPlayer player;
public void PreparePlayer(String filePath)
{
if (player == null)
{
player = new MediaPlayer();
}
player.Reset();
player.SetDataSource(filePath);
player.Prepare();
seekBar1.Max = player.Duration;
}
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
seekBar1.Progress =player.CurrentPosition ;
}
}
}