Overview
Xamarin is a development platform that allows us to code native, cross-platform iOS, Android, and Windows Phone apps in C#. This tutorial focuses on playing the audio file and video file. Please ensure that you are aware of C# class and variables declaration and passing methods.
Requirements
- Visual Studio RC 2017
- Xamarin Studio Packages should be installed
- Self-confidence with creativity
The following steps should be followed.
Step 1
Launch the Visual Studio RC 2017 and go to File >> New >> Project.
Step 2
Then, you need to select Android template from the list, name the application, and press to create the Project and select where to save the project.
Step 3
Here we go. Our new project has been created and we need to navigate to Designer Page. To open the designer page, you need to open Solution Explorer View >>Solution Explorer.
Step 4
Then, to open Designer Page, we need to select the following, Solution Explorer >> Resources >> Layout >> Main.Xaml.
Step 5
Add the following code in Main.Xaml Source Page. This main.xaml file is our designer page and here, our Intent Action will be present available. Need to drag and drop the button, and add the VideoView from toolbox.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/MyButton"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Play This Music"
- android:background="blue" />
- <VideoView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/videoView1"
- android:background=""
- android:theme="" />
- </LinearLayout>
Note: Add the Subfolder Raw under the Resources folder and add the music file in this folder.
Step 5A
Adding the Internet permission for playing our Video and Audio.
Step 6
Add the following code in MainActivity.cs. This main acitivity is what plays the most important role and this makes our app work correctly and customizes buttons with background activity.
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Android.Media;
-
- namespace AudioCracker
- {
- [Activity(Label = "AudioCracker", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- MediaPlayer player;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
-
- SetContentView(Resource.Layout.Main);
-
-
-
- player = MediaPlayer.Create(this,Resource.Raw.Neruppu);
- Button button = FindViewById<Button>(Resource.Id.MyButton);
- button.Click += delegate {
- player.Start();
- };
- var videoView = FindViewById<VideoView>(Resource.Id.videoView1);
- var uri = Android.Net.Uri.Parse("https://www.youtube.com/watch?v=wg-kEWsL6Xc");
- videoView.SetVideoURI(uri);
- videoView.Start();
-
-
-
- }
- }
- }
Final Step
Then, run the app using Visual Studio Android Emulator.
Output
This is the output of Audio and Video files playing in our project and then, successfully executed and played.
Conclusion
Thank you for reading. If you have any doubts, comment below. If you like it, then follow me for more updates about Xamarin Android App development.