Playing The Audio And Video File Using Xamarin Android Application

This tutorial focus on playing audio and video files.

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.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.     <Button  
  7.         android:id="@+id/MyButton"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Play This Music"  
  11.         android:background="blue" />  
  12.     <VideoView  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/videoView1"  
  16.         android:background=""  
  17.         android:theme="" />  
  18. </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. 

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using Android.Media;  
  9.   
  10. namespace AudioCracker  
  11. {  
  12.     [Activity(Label = "AudioCracker", MainLauncher = true, Icon = "@drawable/icon")]  
  13.     public class MainActivity : Activity  
  14.     {  
  15.         MediaPlayer player;  
  16.         protected override void OnCreate(Bundle bundle)  
  17.         {  
  18.             base.OnCreate(bundle);  
  19.   
  20.             // Set our view from the "main" layout resource  
  21.             SetContentView(Resource.Layout.Main);  
  22.   
  23.             // Get our button from the layout resource,  
  24.             // and attach an event to it  
  25.             player = MediaPlayer.Create(this,Resource.Raw.Neruppu);  
  26.             Button button = FindViewById<Button>(Resource.Id.MyButton);  
  27.             button.Click += delegate {  
  28.                 player.Start();  
  29.             };  
  30.             var videoView = FindViewById<VideoView>(Resource.Id.videoView1);  
  31.             var uri = Android.Net.Uri.Parse("https://www.youtube.com/watch?v=wg-kEWsL6Xc");  
  32.             videoView.SetVideoURI(uri);  
  33.             videoView.Start();  
  34.   
  35.               
  36.   
  37.         }  
  38.     }  
  39. }  

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.